<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理页面</title>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="js/index.js"></script>
    <link rel="stylesheet" href="css/guanliyemian.css">
</head>

<body>
    <!-- 背景 -->
    <div class="back">
        <img src="images\gugongh.jpg">
    </div>
    <div class="header">
        <h2>吏部管理台<a href="index.html" id="user"></a></h2>
    </div>
    <div class="add-people-header">
        <h2>添加官职信息</h2>
    </div>
    <div class="add-people-body">
        <span class="input-name">姓名</span>
        <input type="text" class="input-text" id="name" placeholder="姓名">
        <span class="input-name">籍贯</span>
        <input type="text" class="input-text" id="place" placeholder="籍贯">
        <span class="input-name">年龄</span>
        <input type="text" class="input-text" id="age" placeholder="年龄">
        <span class="input-name">俸禄</span>
        <input type="text" class="input-text" id="money" placeholder="俸禄">
    </div>
    <div class="add-foot">
        <button class="add-bit" id="add" onclick="addTr()">添加</button>
    </div>
    <div class="caozuo">
        <div class="xinxi">
            <h3>员工信息</h3>
        </div>
        <div class="biaodan">
            <table cellspacing="0">
                <thead>
                    <tr>
                        <th>姓名</th>
                        <th>籍贯</th>
                        <th>年龄</th>
                        <th>俸禄</th>
                        <th>操作</th>

                    </tr>
                </thead>
                <tbody id="tbody">

                </tbody>
            </table>
        </div>
    </div>
    <div class="bianji">
        <h3>修改员工信息</h3>
        <form class="form-inline">
            <div class="form-group">
                <label for="名字" class="ltext">姓名:</label>
                <input type="text" class="bianji-text" placeholder="名字">
            </div>
            <div class="form-group">
                <label for="籍贯" class="ltext">籍贯:</label>
                <input type="text" class="bianji-text" placeholder="籍贯">
            </div>
            <div class="form-group">
                <label for="年龄" class="ltext">年龄:</label>
                <input type="text" class="bianji-text" placeholder="年龄">
            </div>
            <div class="form-group">
                <label for="俸禄" class="ltext">俸禄:</label>
                <input type="text" class="bianji-text" placeholder="薪资">
            </div>
        </form>
        <button type="button" class="qd-bt bt" id="sure">确定</button>
        <button type="button" class="qx-bt bt" id="close">取消</button>
    </div>
    <div id="bg" class="login-bg"></div>
    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery
/jquery-1.4.min.js"></script>

    <script src="http://code.changer.hk/jquery/plugins/jquery.cookie.js"></script>
    <script>



        var search = location.search;
        console.log(search)

        var str = search.substring(1);

        document.getElementById('user').innerText = '用户' + str + '点击退出';

        var datas = [{
            name: '朱元璋',
            place: '紫禁城',
            age: 20,
            money: 1
        }, {
            name: '于谦',
            place: '北京',
            age: 30,
            money: 1
        }, {
            name: '张居正',
            place: '北京',
            age: 40,
            money: 1
        }];
        localStorage.setItem(1, ['朱元璋', '紫禁城', 20,
            1]);

        //往tbody里创建行
        var tbody = document.querySelector('tbody');
        for (var i = 0; i < datas.length; i++) {
            var tr = document.createElement('tr');
            tbody.appendChild(tr);
            //行里面创建单元格
            for (var k in datas[i]) {
                //创建单元格
                var td = document.createElement('td');
                td.innerHTML = datas[i][k];
                tr.appendChild(td);

            }
            //创建有删除的单元格
            var td = document.createElement('td');

            td.innerHTML = '<button class="qx-bt bt" id="del">删除</button><button class="qd-bt bt" id="change" index="' + i + '"">编辑</button> '//按钮
            tr.appendChild(td);
        }

        var as = document.querySelectorAll('#del');
        for (var i = 0; i < as.length; i++) {
            as[i].onclick = function () {
                tbody.removeChild(this.parentNode.parentNode);

            }
        }
        //添加元素
        var add = document.querySelector('#add');
        var input = document.querySelectorAll('.input-text');

        function addTr() {
            var tr = document.createElement('tr');
            tbody.appendChild(tr);
            //利用循环添加

            for (var i = 0; i <= input.length; i++) {
                if (i < input.length) {
                    var td = document.createElement('td');
                    //如果年龄不符合实际
                    if (input[2].value < 0 || input[2].value > 100) {
                        alert("年龄输入有误，请重新输入");
                        for (var i = 0; i <= input.length; i++) {
                            input[i].value = ''
                        }
                        return false;
                    } else if (input[0].value == '') {
                        alert("姓名不能为空")
                        for (var i = 0; i <= input.length; i++) {
                            input[i].value = ''
                        }
                        break;
                    } else if (input[1].value == "") {
                        alert("籍贯不能为空")
                        for (var i = 0; i <= input.length; i++) {
                            input[i].value = ''
                        }
                        break;
                    } else if (input[2].value == '') {
                        alert("年龄不能为空")
                        for (var i = 0; i <= input.length; i++) {
                            input[i].value = ''
                        }
                        break;
                    } else if (input[3].value == '') {
                        alert("俸禄不能为空")
                        for (var i = 0; i <= input.length; i++) {
                            input[i].value = ''
                        }
                        break;
                    } else {
                        td.innerHTML = input[i].value
                        tr.appendChild(td)

                    }

                } else if (i = input.length) {
                    var td = document.createElement('td');
                    td.innerHTML = '<button class="qx-bt bt" id="del">删除</button><button class="qd-bt bt" id="change">编辑</button> '
                    tr.appendChild(td);
                }
            }
            var as = document.querySelectorAll('#del');
            for (var i = 0; i < as.length; i++) {
                as[i].onclick = function () {
                    tbody.removeChild(this.parentNode.parentNode);
                }
            }
            var change = document.querySelectorAll('#change');
            var sureBtn = document.querySelector('#sure');//确认按钮
            var showValue1 = document.querySelectorAll('.bianji-text');//弹窗文本
            // let index = ""
            var trContent1 = ""
            for (let i = 0; i < change.length; i++) {

                change[i].addEventListener('click', function () {
                    //获取tr
                    trContent1 = change[i].parentNode.parentNode
                    //获取td
                    var td = trContent1.cells
                    var cellValues1 = [];
                    showDiv.style.display = 'block'
                    for (var k = 0; k < showValue1.length; k++) {
                        showValue1[k].value = td[k].textContent
                    }

                    // index = this.getAttribute("index")



                })
                sureBtn.addEventListener('click', function () {

                    //获取tr
                    // var trContent1 = change[index].parentNode.parentNode


                    //获取td
                    var td = trContent1.cells
                    for (let k = 0; k < showValue1.length; k++) {
                        td[k].innerText = showValue1[k].value
                        mask.style.display = 'none';
                        showDiv.style.display = 'none';
                    }
                })


            }


        }
        //编辑修改元素
        var change = document.querySelectorAll('#change');//编辑按钮
        // console.log(change);
        var showDiv = document.querySelector('.bianji');
        var showValue = document.querySelectorAll('.bianji-text');//弹窗文本
        var mask = document.querySelector('.login-bg');
        var closeBtn = document.querySelector('#close');
        var sureBtn = document.querySelector('#sure');//确认按钮

        var input = document.querySelectorAll('.input-text');//添加时的文本
        var tr = document.querySelectorAll('tbody tr');//获取tbody下的tr

        // let index = ""
        var trContent
        for (let i = 0; i < change.length; i++) {

            change[i].addEventListener('click', function () {
                //获取tr
                trContent = change[i].parentNode.parentNode
                //获取td
                var td = trContent.cells
                var cellValues = [];
                showDiv.style.display = 'block'
                for (var k = 0; k < showValue.length; k++) {
                    showValue[k].value = td[k].textContent
                }
                index = this.getAttribute("index")

            })
            sureBtn.addEventListener('click', function () {
                //获取tr
                // var trContent = change[index].parentNode.parentNode
                //获取td
                var td = trContent.cells
                for (let k = 0; k < showValue.length; k++) {
                    td[k].innerText = showValue[k].value
                    mask.style.display = 'none';
                    showDiv.style.display = 'none';
                }
            })
        }
        closeBtn.addEventListener('click', function () {
            mask.style.display = 'none';
            showDiv.style.display = 'none';
        })

    </script>

</body>

</html>